package com.zyx.javademo.collection.map;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author Yaxi.Zhang
 * @since 2021/7/28 12:05
 * desc: TreeMap案例
 */
public class TreeMapDemo {
    public static void main(String[] args) {
        TreeMap<Double, Long> treeMap = new TreeMap<>(Comparator.reverseOrder());
        treeMap.put(2.00, 15L);
        treeMap.put(3.00, 20L);
        treeMap.put(2.50, 30L);
        treeMap.put(2.15, 40L);
        treeMap.put(2.75, 50L);

        System.out.println("treeMap ==> " + treeMap);

        // 筛选出位于toKey之后的所有元素
        // 第二个参数: true(默认)表示包含toKey元素; false表示不包含toKey元素
        SortedMap<Double, Long> tailMap = treeMap.tailMap(2.99);
        System.out.println("tailMap ==> " + tailMap);

        // 筛选出位于toKey之前的所有元素
        // 第二个参数: true表示包含toKey元素; false(默认)表示不包含toKey元素
        SortedMap<Double, Long> headMap = treeMap.headMap(3.00);
        System.out.println("headMap ==> " + headMap);


        Double ceilingKey = treeMap.ceilingKey(2.75);
        System.out.println("ceilingKey ==> " + ceilingKey);

        Double higherKey = treeMap.higherKey(2.70);
        System.out.println("higherKey ==> " + higherKey);

        System.out.println("descendingKeySet ==> " + treeMap.descendingKeySet());
    }
}
